//app.js
var domain = "http://10.1.1.17:8080"; //接口目录
var errimg = "/images/error.jpg"; //错误图
App({
  onLaunch: function () {

    var that = this;
    wx.checkSession({
      success: function () {
        //存在登陆态
        that.login();
      },
      fail: function () {
        //不存在登陆态
        wx.clearStorage();
        that.login();
      }
    })
  },
  
  login: function () {

    var that = this;

    //用户授权信息
    var loginInformation = wx.getStorageSync('userInfo'); //用户登录信息
    var sessionKey = wx.getStorageSync('sessionKey'); //第一次登录缓存信息
    var dealer = wx.getStorageSync('dealer'); //选择的供应商信息


    var token = wx.getStorageSync('token');
    console.log(token)

    if (token) { //检测token
      return true;
    }else{
      wx.reLaunch({
        url: '/pages/home/login/login'
      })
    }
    
  },
  onLoad: function () {

  },
  globalData: {
    userInfo: null,
    domain: domain,
    errimg: errimg
  },
  globalAjax: function (url, method, data, call, head) { //全局接口方法 1url地址，2接口类型，3data参数，4回调，5header
    var data = data || {};
    var method = method || "get";
    var head1 = {
      token: wx.getStorageSync('token')
    };
    console.log(head1)
    wx.showNavigationBarLoading();
    wx.request({
      url: domain + url,
      header: head1,
      data: data,
      method: method,
      success: function (res) {
        if (typeof res.data === "object") {
          call(res.data)
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '接口加载错误',
          icon: "none"
        });
        // fail调用接口失败
      },
      complete: function () {
        wx.hideNavigationBarLoading();
      }
    });
  },
  pays: function (data, call, call2) { //同意支付调用
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.package,
      signType: 'MD5',
      paySign: data.paySign,
      fail: function (aaa) {
        wx.showToast({
          title: '支付失败',
          icon: "none"
        })
        call2(aaa);
      },
      success: function (e) {
        wx.showToast({
          title: '支付成功'
        })
        call(e);
      }
    })
  }
})